package org.activiti.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dfhc.ISystemConstant;
import com.dfhc.pub.service.FlowJsonToXmlService;
import com.dfhc.util.StringHelper;
import com.dfhc.wk.frontflowdef.service.FrontFlowDefService;
import com.dfhc.wk.frontflowdef.vo.FrontFlowDefVo;
import com.dfhc.wk.workflownodeconfig.service.WorkflowNodeConfigService;
import com.dfhc.wk.workflownodeconfig.vo.WorkflowNodeConfigVo;

/**
 * 前台流程设计控制器
 * 
 * @author henryyan
 */
@Controller
@RequestMapping(value = "/frontDesign")
public class FrontDesignController {

	@Autowired
	FlowJsonToXmlService flowJsonToXmlService;
	@Autowired
	FrontFlowDefService frontFlowDefService;
	@Autowired
	WorkflowNodeConfigService workflowNodeConfigService;
	
	  /**
     * 前台流程设计
     * */
    @RequestMapping(value="")
    public String frontDesign(Model model,HttpServletRequest request){
    	Map<String,Object> searchPara = new HashMap<String,Object>();
    	
    	
    	List<WorkflowNodeConfigVo> vos = workflowNodeConfigService.list(searchPara, null);
  	  
    	model.addAttribute("vos", vos);
    	
    	String procDefId = request.getParameter("procDefId");
    	if(StringHelper.isEmpty(procDefId)){
    		model.addAttribute("procDefContent",  "({states:{},paths:{},props:{props:{processId:{value:''},name:{value:''},documentation:{value:''},formTemplateName:{value:''},formTemplateId:{value:''}}}})");
        	return "/frontDesign/frontDesign";
    	}
    	searchPara.clear();
    	searchPara.put("procDefId",procDefId);
    	
    	String procDefContent = "";
    	
	   	List<FrontFlowDefVo> frontFlowDefVos = frontFlowDefService.list(searchPara, null);
	   	if(!CollectionUtils.isEmpty(frontFlowDefVos)){
	   		procDefContent = "("+frontFlowDefVos.get(0).getProcDefContent()+")";
	   		procDefContent = StringHelper.replaceAll(procDefContent, "\"", "\\\"").toString();
//	   		procDefContent = "({states:{rect1:{type:'start',text:{text:'开始1'}, attr:{ x:149, y:98, width:50, height:50}, props:{name:{value:'开始1'}}},rect2:{type:'check',text:{text:'审核2'}, attr:{ x:116, y:219, width:100, height:50}, props:{name:{value:'审核2'},documentation:{value:'审核'},candidateGroups:{value:'{"userList":[{"name":"张三","id":"2011201611210000002","selectmain":false}],"roleList":[]}'}}},rect3:{type:'end',text:{text:'结束3'}, attr:{ x:147, y:343, width:50, height:50}, props:{name:{value:'结束3'},documentation:{value:''}}}},paths:{path4:{from:'rect1',to:'rect2', dots:[],text:{text:'TO 审核2',textPos:{x:0,y:-10}}, props:{text:{value:''}}},path5:{from:'rect2',to:'rect3', dots:[],text:{text:'TO 结束3',textPos:{x:0,y:-10}}, props:{text:{value:''}}}},props:{props:{processId:{value:'ZD_DFHC_5993bbb4-26df-4134-8c96-c7fbb6e78d1a'},name:{value:'20177261517'},documentation:{value:''}}}})";
	   	
	   	}
	   	if(StringHelper.isEmpty(procDefContent)){
	   		procDefContent = "({states:{},paths:{},props:{props:{processId:{value:''},name:{value:''},documentation:{value:''},formTemplateName:{value:''},formTemplateId:{value:''}}}})";
	   	}
	   	
		model.addAttribute("procDefContent", procDefContent);
    	return "/frontDesign/frontDesign";
    }
    /**
     * 保存前台设计的流程
     * */
    @RequestMapping(value="doSaveProcess", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> doSaveProcess(HttpServletRequest request){
  	  Map<String,Object> result=new HashMap<String,Object>();
  	  try {
  		
  		  String  jsonStr=request.getParameter("jsonStr");
  		  flowJsonToXmlService.doDeployFlow(jsonStr);
  		  result.put(ISystemConstant.AJAX_STATUS,ISystemConstant.AJAX_RESULT_SUCCESS);
  		  result.put(ISystemConstant.AJAX_MESSAGE,"保存成功");
  		} catch (Exception e) {
  			// TODO: handle exception
  			  result.put(ISystemConstant.AJAX_STATUS,ISystemConstant.AJAX_RESULT_FAIL);
  			  result.put(ISystemConstant.AJAX_MESSAGE,"保存失败！开始节点未添加属性，或其他节点属性是否错误！");
  		}
  	  
  	  return result;
    }
}
